package com.example.xiaomiao.Service.Impl;

import com.example.xiaomiao.DO.Money_Record;
import com.example.xiaomiao.DO.Note;
import com.example.xiaomiao.DTO.*;
import com.example.xiaomiao.Mapper.MoneyRecordMapper;
import com.example.xiaomiao.Service.MoneyRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MoneyRecordServiceImpl implements MoneyRecordService {
    @Resource
    MoneyRecordMapper moneyRecordMapper;
    private static final Logger logger = LoggerFactory.getLogger(MoneyRecordServiceImpl.class);

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Return add(int userId, MoneyAddRequestDTO moneyAddRequestData) {
        try {
            return add1(userId,moneyAddRequestData);
        } catch (Exception e){
            return Return.fail(1,"添加失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Return delete(MoneyDeleteRequestDTO moneyDeleteRequestData) {
        try {
            return delete1(moneyDeleteRequestData);
        } catch (Exception e){
            return Return.fail(1,"删除失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Return update(Money_Record moneyRecord) {
        try {
            return update1(moneyRecord);
        } catch (Exception e){
            return Return.fail(1,"修改失败");
        }
    }

    @Override
    public Return get(int userId, GetMoneyRequestDTO getMoneyRequestData) {
        int total = moneyRecordMapper.getMoneyRecordNum(userId,getMoneyRequestData.getStart_date(),getMoneyRequestData.getEnd_date());
        if(total == 0)
            return Return.ok("还没有收支记录",new ListResponseDTO(total,new ArrayList<>()));
        int start = getMoneyRequestData.getStart_index();
        int count = getMoneyRequestData.getCount();
        List<Money_Record> moneyRecords = moneyRecordMapper.getMoneyRecords(userId,getMoneyRequestData.getStart_date(),getMoneyRequestData.getEnd_date(),start,Math.min(count,total-start));
        if(moneyRecords.isEmpty())
            return Return.ok("这一页还没有收支记录",new ListResponseDTO(total,moneyRecords));
        return Return.ok(new ListResponseDTO(total,moneyRecords));
    }

    @Transactional(rollbackFor = Exception.class)
    public Return add1(int userId, MoneyAddRequestDTO moneyAddRequestData) {
        try{
            moneyRecordMapper.addMoneyRecords(userId,moneyAddRequestData.getDate(),moneyAddRequestData.getMoney_records());
            return Return.ok();
        } catch (Exception e){
            logger.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public Return delete1(MoneyDeleteRequestDTO moneyDeleteRequestData) {
        try {
            moneyRecordMapper.deleteMoneyRecords(moneyDeleteRequestData.getMoney_ids());
            return Return.ok();
        } catch (Exception e){
            logger.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public Return update1(Money_Record moneyRecord) {
        try {
            moneyRecordMapper.updateMoneyRecord(moneyRecord);
            return Return.ok();
        } catch (Exception e){
            logger.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }
}
